/**
 * Created by qianjy on 2016/11/29.
 */
require.config(window.jshead);
require(['jquery', 'pages','layer', 'navigator'], function ($, pg,layer) {

    var _index=0;
    var index_=0;
    couponList({status:2});
    ticketList({status:2});

    $('#dont').bind('click',function(){
        index_=0;
            if(_index==0){
                couponList({status:2});
            }else{
                ticketList({status:2});
            }
        $('#dont').css('color','#ff6262');
        $('#been').css('color','#999999');
        $('#overdue').css('color','#999999');
    });
    $('#been').bind('click',function(){
        index_=1;

            if(_index==0){
                couponList({status:1},callback);
            }else{
                ticketList({status:1},overdue);
            }

        $('#dont').css('color','#999999');
        $('#been').css('color','#ff6262');
        $('#overdue').css('color','#999999');
    });
    $('#overdue').bind('click',function(){
        index_=2;
            if(_index==0){
                couponList({status:3},callback2);
            }else{
                ticketList({status:3},overdue2);
            }

        $('#dont').css('color','#999999');
        $('#been').css('color','#999999');
        $('#overdue').css('color','#ff6262');
    });
    //邀请列表
    function couponList(parms,callback){
        pg.paging(1, 8, "/user/reward/couponList",'rewardList','rewardListTemp',parms,'#pages2',callback);

    }
    function callback(){
        $('.redenvelope').css('background','url("/img/yiyonghongbao.png") no-repeat center');
        $('.use').addClass('display');
        $('.employ').html('已使用');
        $('.employ').removeAttr('href');
        $('.employ-but').css('padding-left','50px');
        $('.employ').css('color','#909090');
    }
    function callback2(){
        $('.redenvelope').css('background','url("/img/yiyonghongbao.png") no-repeat center');
        $('.use').addClass('display');
        $('.employ').html('已过期');
        $('.employ').removeAttr('href');
        $('.employ-but').css('padding-left','50px');
        $('.employ').css('color','#909090');
    }
    function ticketList(parms,overdue){
        pg.paging(1, 8, "/user/reward/ticketList",'redenvelopeList','redenvelopeListTemp',parms,'#pages',overdue);
    }
    function overdue(){
        $('.redenvelope').css('background','url("/img/yiyongjiaxiquan.png") no-repeat center');
        $('.use').addClass('display');
        $('.employ').html('已使用');
        $('.employ').removeAttr('href');
        $('.employ-but').css('padding-left','50px');
        $('.employ').css('color','#909090');
    }

    function overdue2(){
        $('.redenvelope').css('background','url("/img/yiyongjiaxiquan.png") no-repeat center');
        $('.use').addClass('display');
        $('.employ').html('已过期');
        $('.employ').removeAttr('href');
        $('.employ-but').css('padding-left','50px');
        $('.employ').css('color','#909090');
    }


    $('.title-red').bind('click',function(){
        _index=0;
        if(index_==1){
            couponList({status:1},callback);
        }else if(index_==2){
            couponList({status:3},callback2);
        }else{
            couponList({status:2});
        }
        $('#red').removeClass('display');
        $('#rate').addClass('display');
        $('.title-red').addClass('reward_active');
        $('.title-rate').removeClass('reward_active');

    });

    $('.title-rate').bind('click',function(){
        _index=1;
        if(index_==1){
            ticketList({status:1},overdue);
        }else if(index_==2){
            ticketList({status:3},overdue2);
        }else{
            ticketList({status:2});
        }
        $('#red').addClass('display');
        $('#rate').removeClass('display');
        $('.title-rate').addClass('reward_active');
        $('.title-red').removeClass('reward_active');
    });


    $('#exchange').bind('click',function(){
        var exchange=$('#exchanText').val();
        if(exchange==''){
            layer.tips('请先输入邀请码','#exchanText');
            return
        }
        $.ajax({
            type: "POST",
            url: "/user/reward/inviteBinding",
            data: {
                inviteCode:exchange
            },
            dataType: "json",
            success: function (data) {

                if(data.result=='success'){
                    layer.alert('兑换成功');
                }else{
                    layer.alert(data.messageText);
                }
            }
        });
    })

});